<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Script payload transform | ElasticSearch 7.7 权威指南中文版</title>
	<meta name="keywords" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <meta name="description" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
	<script>
	var _link = 'transform-script.html';
    </script>
</head>
<body>
<div class="main-container">
    <section id="content">
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.7/transform-script.html" rel="nofollow" target="_blank">https://www.elastic.co/guide/en/elasticsearch/reference/7.7/transform-script.html</a>, 原文档版权归 www.elastic.co 所有<br/>本地英文版地址: <a href="../en/transform-script.html" rel="nofollow" target="_blank">../en/transform-script.html</a></div>
                        <!-- start body -->
                  <div class="page_header">
<strong>重要</strong>: 此版本不会发布额外的bug修复或文档更新。最新信息请参考 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html" rel="nofollow">当前版本文档</a>。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch Guide [7.7]</a></span>
»
<span class="breadcrumb-link"><a href="xpack-alerting.html">Alerting on cluster and index events</a></span>
»
<span class="breadcrumb-link"><a href="transform.html">Payload transforms</a></span>
»
<span class="breadcrumb-node">Script payload transform</span>
</div>
<div class="navheader">
<span class="prev">
<a href="transform-search.html">« Search payload transform</a>
</span>
<span class="next">
<a href="transform-chain.html">Chain payload transform »</a>
</span>
</div>
<div class="section xpack">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="transform-script"></a>Script payload transform<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/x-pack/docs/en/watcher/transform/script.asciidoc">edit</a><a class="xpack_tag" href="https://www.elastic.co/subscriptions"></a>
</h2>
</div></div></div>
<p>A <a class="xref" href="transform.html" title="Payload transforms">payload transform</a> that executes a script on the current
payload in the watch execution context and replaces it with a newly generated
one. The following snippet shows how a simple script payload transform can be
defined on the watch level:</p>
<div class="tip admon">
<div class="icon"></div>
<div class="admon_content">
<p>The <code class="literal">script</code> payload transform is often useful when used in combination
      with the <a class="xref" href="transform-search.html" title="Search payload transform"><code class="literal">search</code></a> payload transform, where the
      script can extract only the significant data from a search result, and by
      that, keep the payload minimal. This can be achieved with the
      <a class="xref" href="transform-chain.html" title="Chain payload transform"><code class="literal">chain</code></a> payload transform.</p>
</div>
</div>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
  "transform" : {
    "script" : "return [ 'time' : ctx.trigger.scheduled_time ]" <a id="CO533-1"></a><i class="conum" data-value="1"></i>
  }
}</pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO533-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>A simple <code class="literal">painless</code> script that creates a new payload with a single <code class="literal">time</code>
field holding the scheduled time.</p>
</td>
</tr>
</table>
</div>
<div class="note admon">
<div class="icon"></div>
<div class="admon_content">
<p>The executed script may either return a valid model that is the equivalent
      of a Java™ Map or a JSON object (you will need to consult the
      documentation of the specific scripting language to find out what this
      construct is). Any other value that is returned will be assigned and
      accessible to/via the <code class="literal">_value</code> variable.</p>
</div>
</div>
<p>The <code class="literal">script</code> attribute may hold a string value in which case it will be treated
as an inline script and the default elasticsearch script languages will be assumed
(as described in <a class="xref" href="modules-scripting.html" title="Scripting">Scripting</a>). You can
use the other scripting languages supported by Elasticsearch. For this, you need
to set the <code class="literal">script</code> field to an object describing the script and its language.
The following table lists the possible settings that can be configured:</p>
<div class="table">
<a id="transform-script-settings"></a>
<p class="title"><strong>Table 94. Script payload transform settings</strong></p>
<div class="table-contents">
<table border="1" cellpadding="4px" summary="Script payload transform settings">
<colgroup>
<col class="col_1">
<col class="col_2">
<col class="col_3">
<col class="col_4">
</colgroup>
<thead>
<tr>
<th align="left" valign="top">Name</th>
<th align="left" valign="top">Required</th>
<th align="left" valign="top">Default</th>
<th align="left" valign="top">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left" valign="top"><p><code class="literal">inline</code></p></td>
<td align="left" valign="top"><p>yes</p></td>
<td align="left" valign="top"><p>-</p></td>
<td align="left" valign="top"><p>When using an inline script, this field holds
                                     the script itself.</p></td>
</tr>
<tr>
<td align="left" valign="top"><p><code class="literal">id</code></p></td>
<td align="left" valign="top"><p>yes</p></td>
<td align="left" valign="top"><p>-</p></td>
<td align="left" valign="top"><p>When referring to a stored script, this
                                     field holds the id of the script.</p></td>
</tr>
<tr>
<td align="left" valign="top"><p><code class="literal">lang</code></p></td>
<td align="left" valign="top"><p>no</p></td>
<td align="left" valign="top"><p><code class="literal">painless</code></p></td>
<td align="left" valign="top"><p>The script language</p></td>
</tr>
<tr>
<td align="left" valign="top"><p><code class="literal">params</code></p></td>
<td align="left" valign="top"><p>no</p></td>
<td align="left" valign="top"><p>-</p></td>
<td align="left" valign="top"><p>Additional parameters/variables that are
                                     accessible by the script</p></td>
</tr>
</tbody>
</table>
</div>
</div>
<p>When using the object notation of the script, one (and only one) of <code class="literal">inline</code>,
or <code class="literal">id</code> fields must be defined.</p>
<div class="note admon">
<div class="icon"></div>
<div class="admon_content">
<p>In addition to the provided <code class="literal">params</code>, the scripts also have access to the
      <a class="xref" href="how-watcher-works.html#watch-execution-context" title="Watch execution context">standard watch execution context parameters</a>.</p>
</div>
</div>
</div>
<div class="navfooter">
<span class="prev">
<a href="transform-search.html">« Search payload transform</a>
</span>
<span class="next">
<a href="transform-chain.html">Chain payload transform »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>